﻿/*********************************************************************
 版权所有: 版权所有(C) 2010 ,熙软研发部
 模块编号:  ContactGroupDO
 作    者:范徐超
 编写日期:2010-3-6
 审    核:
 审核日期:
 描    述:ContactGroupDO 通讯录联系人组  数据访问类
...........................................................................
如果模块有修改，则每次修改添加以下注释(以系统上线后为基准)：

 *********************************************************************/

using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;

using Cisoft.IntegrationSystem.DataHelper;

namespace Cisoft.Cisoft3G.DAL
{
    /// <summary>
    /// 数据访问类ContactGroupDO。
    /// </summary>
    public class ContactGroupDO
    {
        public ContactGroupDO()
        { }
        #region  成员方法

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return SQLServerHelper.GetMaxID("CG_ContactGroupID", "CG_ContactGroup");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int CG_ContactGroupID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from CG_ContactGroup");
            strSql.Append(" where CG_ContactGroupID=@CG_ContactGroupID ");
            SqlParameter[] parameters = {
					new SqlParameter("@CG_ContactGroupID", SqlDbType.Int,4)};
            parameters[0].Value = CG_ContactGroupID;

            return SQLServerHelper.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(Cisoft.Cisoft3G.Model.ContactGroupMO model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into CG_ContactGroup(");
            strSql.Append("CG_ContactGroupID,CG_GroupName,CG_ParentID,CG_Order)");
            strSql.Append(" values (");
            strSql.Append("@CG_ContactGroupID,@CG_GroupName,@CG_ParentID,@CG_Order)");
            SqlParameter[] parameters = {
					new SqlParameter("@CG_ContactGroupID", SqlDbType.Int,4),
					new SqlParameter("@CG_GroupName", SqlDbType.NVarChar,50),
					new SqlParameter("@CG_ParentID", SqlDbType.Int,4),
					new SqlParameter("@CG_Order", SqlDbType.Int,4)};
            parameters[0].Value = model.CG_ContactGroupID;
            parameters[1].Value = model.CG_GroupName;
            parameters[2].Value = model.CG_ParentID;
            parameters[3].Value = model.CG_Order;

            SQLServerHelper.ExecuteSql(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(Cisoft.Cisoft3G.Model.ContactGroupMO model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update CG_ContactGroup set ");
            strSql.Append("CG_GroupName=@CG_GroupName,");
            strSql.Append("CG_ParentID=@CG_ParentID,");
            strSql.Append("CG_Order=@CG_Order");
            strSql.Append(" where CG_ContactGroupID=@CG_ContactGroupID ");
            SqlParameter[] parameters = {
					new SqlParameter("@CG_ContactGroupID", SqlDbType.Int,4),
					new SqlParameter("@CG_GroupName", SqlDbType.NVarChar,50),
					new SqlParameter("@CG_ParentID", SqlDbType.Int,4),
					new SqlParameter("@CG_Order", SqlDbType.Int,4)};
            parameters[0].Value = model.CG_ContactGroupID;
            parameters[1].Value = model.CG_GroupName;
            parameters[2].Value = model.CG_ParentID;
            parameters[3].Value = model.CG_Order;

            SQLServerHelper.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int CG_ContactGroupID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from CG_ContactGroup ");
            strSql.Append(" where CG_ContactGroupID=@CG_ContactGroupID ");
            SqlParameter[] parameters = {
					new SqlParameter("@CG_ContactGroupID", SqlDbType.Int,4)};
            parameters[0].Value = CG_ContactGroupID;

            SQLServerHelper.ExecuteSql(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Cisoft.Cisoft3G.Model.ContactGroupMO GetModel(int CG_ContactGroupID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 CG_ContactGroupID,CG_GroupName,CG_ParentID,CG_Order from CG_ContactGroup ");
            strSql.Append(" where CG_ContactGroupID=@CG_ContactGroupID ");
            SqlParameter[] parameters = {
					new SqlParameter("@CG_ContactGroupID", SqlDbType.Int,4)};
            parameters[0].Value = CG_ContactGroupID;

            Cisoft.Cisoft3G.Model.ContactGroupMO model = new Cisoft.Cisoft3G.Model.ContactGroupMO();
            DataSet ds = SQLServerHelper.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["CG_ContactGroupID"].ToString() != "")
                {
                    model.CG_ContactGroupID = int.Parse(ds.Tables[0].Rows[0]["CG_ContactGroupID"].ToString());
                }
                model.CG_GroupName = ds.Tables[0].Rows[0]["CG_GroupName"].ToString();
                if (ds.Tables[0].Rows[0]["CG_ParentID"].ToString() != "")
                {
                    model.CG_ParentID = int.Parse(ds.Tables[0].Rows[0]["CG_ParentID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["CG_Order"].ToString() != "")
                {
                    model.CG_Order = int.Parse(ds.Tables[0].Rows[0]["CG_Order"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Cisoft.Cisoft3G.Model.ContactGroupMO GetModel(string strWhere)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 CG_ContactGroupID,CG_GroupName,CG_ParentID,CG_Order from CG_ContactGroup ");
            if (!string.IsNullOrEmpty(strWhere))
            {
                strSql.Append(" where 1=1 and " + strWhere);
            }
            Cisoft.Cisoft3G.Model.ContactGroupMO model = new Cisoft.Cisoft3G.Model.ContactGroupMO();
            DataSet ds = SQLServerHelper.Query(strSql.ToString(), null);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["CG_ContactGroupID"].ToString() != "")
                {
                    model.CG_ContactGroupID = int.Parse(ds.Tables[0].Rows[0]["CG_ContactGroupID"].ToString());
                }
                model.CG_GroupName = ds.Tables[0].Rows[0]["CG_GroupName"].ToString();
                if (ds.Tables[0].Rows[0]["CG_ParentID"].ToString() != "")
                {
                    model.CG_ParentID = int.Parse(ds.Tables[0].Rows[0]["CG_ParentID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["CG_Order"].ToString() != "")
                {
                    model.CG_Order = int.Parse(ds.Tables[0].Rows[0]["CG_Order"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select CG_ContactGroupID,CG_GroupName,CG_ParentID,CG_Order ");
            strSql.Append(" FROM CG_ContactGroup ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return SQLServerHelper.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" CG_ContactGroupID,CG_GroupName,CG_ParentID,CG_Order ");
            strSql.Append(" FROM CG_ContactGroup ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return SQLServerHelper.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "CG_ContactGroup";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return SQLServerHelper.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  成员方法
    }
}

